<?php
require_once 'config.php';
require_once 'db_manager.php';
require_once 'carrier_map.php';
session_start();

// 检查是否已登录
if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit();
}

// 处理删除请求
if (isset($_GET['delete'])) {
    $db = DBManager::getInstance();
    $conn = $db->getConnection();
    $stmt = $conn->prepare("DELETE FROM tracking_history WHERE id = ? AND user_id = ?");
    $stmt->bind_param("ii", $_GET['delete'], $_SESSION['user_id']);
    $stmt->execute();
    $stmt->close();
    
    header("Location: history.php");
    exit();
}

// 获取查询历史
$db = DBManager::getInstance();
$conn = $db->getConnection();
$stmt = $conn->prepare("SELECT * FROM tracking_history WHERE user_id = ? ORDER BY query_time DESC");
$stmt->bind_param("i", $_SESSION['user_id']);
$stmt->execute();
$history = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
?>

<?php include 'header.php'; ?>

<style>
.table-hover tbody tr {
    cursor: pointer;
    transition: background 0.2s;
}
.table-hover tbody tr:hover {
    background: #f0f4ff;
}
.table th {
    text-align: center !important;
    vertical-align: middle;
    font-size: 1rem;
    white-space: nowrap;
    width: 1%;
}
.table td {
    font-size: 0.85rem;
    vertical-align: middle;
}
</style>

<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">
                <h3 class="text-center">查询历史</h3>
            </div>
            <div class="card-body">
                <?php if(empty($history)): ?>
                    <div class="alert alert-info">暂无查询记录</div>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>单号</th>
                                    <th>快递公司</th>
                                    <th>状态</th>
                                    <th>查询时间</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php 
                                $unique_history = array();
                                foreach($history as $item) {
                                    $tracking_number = explode(':', $item['tracking_number'])[0];
                                    if (!isset($unique_history[$tracking_number])) {
                                        $unique_history[$tracking_number] = $item;
                                    }
                                }
                                foreach($unique_history as $item): 
                                    $carrier_name = '';
                                    if (!empty($item['carrier'])) {
                                        $carrier_code = strtolower($item['carrier']);
                                        $carrier_name = isset($carrier_map[$carrier_code]) ? $carrier_map[$carrier_code] : $item['carrier'];
                                    }
                                ?>
                                    <tr onclick="window.location.href='index.php?tracking_number=<?php echo $item['tracking_number']; ?><?php if(!empty($item['carrier'])) echo '&carrier=' . urlencode($item['carrier']); ?>'">
                                        <td><?php echo explode(':', $item['tracking_number'])[0]; ?></td>
                                        <td><?php echo !empty($carrier_name) ? htmlspecialchars($carrier_name) : '未识别'; ?></td>
                                        <td class="history-status">
                                            <?php 
                                            $status = isset($item['status']) ? (string)$item['status'] : '暂无状态';
                                            if (mb_strlen($status, 'UTF-8') > 20) {
                                                echo mb_substr($status, 0, 20, 'UTF-8') . '...';
                                            } else {
                                                echo $status;
                                            }
                                            ?>
                                        </td>
                                        <td><?php echo $item['query_time']; ?></td>
                                        <td>
                                            <a href="history.php?delete=<?php echo $item['id']; ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定要删除这条记录吗？')">删除</a>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

<?php include 'footer.php'; ?> 